package org.adream.account.controller;

import java.util.List;

import org.adream.account.aop.annotation.MonitorRequest;
import org.adream.account.dao.UserRoleLinkDao;
import org.adream.account.entity.RoleEntity;
import org.adream.account.entity.UserEntity;
import org.adream.account.entity.UserRoleLinkEntity;
import org.adream.account.service.ADreamServiceException;
import org.adream.account.service.RoleService;
import org.adream.account.service.UserService;
import org.adream.account.util.Constant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;


/**
 * 用户管理模块相关页面
 * 
 * @author sl
 *
 */
@Controller
public class UserController {

	private final static Logger logger = LoggerFactory.getLogger(UserController.class);

	@Autowired
	private UserService userService;

	@Autowired
	private RoleService roleService;

	@Autowired
	private UserRoleLinkDao userRoleDao;

	@RequestMapping(value = "/userPage")
	@MonitorRequest
	public ModelAndView user(String uid) {
		ModelAndView mv = new ModelAndView("/user/user");
		mv.getModel().put("realNameUid", uid);
		List<RoleEntity> roles = roleService.queryRolesByVerifyAndDr(Constant.ZERO,Constant.ONE);
		mv.addObject("roles", roles);
		return mv;
	}

	@RequestMapping(value = "/userEditPage")
	@MonitorRequest
	public ModelAndView userEdit(String uid) {
		ModelAndView mv = new ModelAndView("/user/user_edit");
		UserEntity user = null;
		user = userService.queryUserByUid(uid);
		List<RoleEntity> roles = roleService.queryRolesByVerifyAndDr(Constant.ZERO,Constant.ONE);
		int[] rleIds = new int[roles.size()];
		int size = roles.size();
		for (int i = 0; i < size; i++) {
			UserRoleLinkEntity userRole = userRoleDao.queryUserRoleById(uid, roles.get(i).getRleId());
			if (userRole != null) {
				rleIds[i] = Constant.ONE;
			}
		}
		mv.getModel().put("user", user);
		mv.getModel().put("roles", roles);
		mv.getModel().put("rleIds", rleIds);
		return mv;
	}
}